//
//  CTDeviceFunction.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/4/3.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTDeviceFunction.h"

//func
#import "CTDebugManager.h"


//frame work
#import <AssetsLibrary/AssetsLibrary.h>


typedef void (^SaveImageCompletion)();

@implementation CTDeviceFunction




+(NSString *)DeviceUDID
{
    NSString * deviceID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    deviceID = [deviceID stringByReplacingOccurrencesOfString:@"-" withString:@""];
    return deviceID;
}


+(void)jumpToAppStore:(NSString *)path
{
    if ([path rangeOfString:@"https://"].location != NSNotFound) {
        path = [path stringByReplacingOccurrencesOfString:@"https://" withString:@""];
    }else if ([path rangeOfString:@"http://"].location != NSNotFound)
    {
        path = [path stringByReplacingOccurrencesOfString:@"http://" withString:@""];
    }
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"itms-apps://%@",path]]];
}

+(NSString *)UnixTimestamp
{
    int time =(int)[[NSDate date] timeIntervalSince1970];
    NSString * timeStamp = [NSString stringWithFormat:@"%d",time];
    return timeStamp;
}

+(UIImage*)imageFromView:(UIView *)theView atFrame:(CGRect)r
{
//    UIGraphicsBeginImageContext(CGSizeMake(r.size.width, r.size.height));
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    CGContextSaveGState(context);
//    UIRectClip(r);
//    [theView.layer renderInContext:context];
//    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    return  theImage;
    //设置屏幕大小
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(theView.bounds.size.width, theView.bounds.size.width), YES, 0);
    //设置截屏大小
    [[theView layer] renderInContext:UIGraphicsGetCurrentContext()];
    
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    CGImageRef imageRef = viewImage.CGImage;
    CGRect rect;
    rect = CGRectMake(0, 0, theView.bounds.size.width*2*1.5,  theView.bounds.size.width*2*1.5);//这里可以设置想要截图的区域
    CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);
    UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRefRect];
    return sendImage;
}

+(UIImage *)cutImg:(UIImage *)img atFrame:(CGRect)r
{
    CGRect rect =  r;
    CGImageRef cgimg = CGImageCreateWithImageInRect([img CGImage], rect);
    UIImage * cutImg = [UIImage imageWithCGImage:cgimg];
    CGImageRelease(cgimg);
   
    return cutImg;
}





#pragma mark device type

+(DeviceTypeAdaptation)deviceTypeForResolutionAdaptation
{
    int height = [UIScreen mainScreen].bounds.size.height;
    int pixelsTimes = [UIScreen mainScreen].scale;
    
    DeviceTypeAdaptation deviceType;
    
    
    if (pixelsTimes * height == 960 || pixelsTimes * height == 480)
    {
        deviceType = iPhone4_4s;
    }
    else if (pixelsTimes * height == 1136)
    {
        deviceType = iPhone5_5s_5c;
    }
    else if (pixelsTimes * height == 1334)
    {
        deviceType = iPhone6;
    }
    else if (pixelsTimes * height == 2001 || pixelsTimes * height == 2208)
    {
        deviceType = iPhone6_Plus;
    }
    else
    {
        deviceType = iPhone_Unknow;
    }
    
    return deviceType;
    
}


+(DeviceSystemVersion)deviceSystemVersion
{
    DeviceSystemVersion  systemVersion = DeviceSystemVersion_Unknow;
    
    if (([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)) {
        systemVersion = DeviceSystemVersion_iOS8;
    }else if (([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)){
        systemVersion = DeviceSystemVersion_iOS7;
    }else if (([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)){
        systemVersion = DeviceSystemVersion_iOS6;
    }else if (([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0)){
        systemVersion = DeviceSystemVersion_iOS5;
    }else{
        systemVersion = DeviceSystemVersion_Unknow;
    }
    
    return systemVersion;
}

//清除通知角标
+(void)clearNotiBadgeNumber
{
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}





#pragma mark - 存入相册 —

+(void)saveImageToAlbum:(NSString *)name withImage:(UIImage *)img
{
     ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
    [library writeImageToSavedPhotosAlbum:img.CGImage orientation:(ALAssetOrientation)img.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error) {
                
        [self addAssetURL:assetURL toAlbum:name withLibrary:library withCompletionBlock:^{
            [CTDebugManager showDebugLogInfo:@"存入相册成功!"];
        }];
    }];
    library = nil;
}

//存入指定相册
+(void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName withLibrary:(ALAssetsLibrary *)library withCompletionBlock:(SaveImageCompletion)completionBlock
{
    //相册存在标示
    __block BOOL albumWasFound = NO;
    
    ALAssetsLibrary *assetsLibrary = library;
    //search all photo albums in the library
    
    [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop)
     {
         
         //判断相册是否存在
         if ([albumName compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) {
             
             //存在
             albumWasFound = YES;
             
             //get a hold of the photo's asset instance
             [assetsLibrary assetForURL: assetURL
                            resultBlock:^(ALAsset *asset) {
                                
                                //add photo to the target album
                                [group addAsset: asset];
                                
                                //run the completion block
                                completionBlock(nil);
                                
                            } failureBlock: completionBlock];
             return;
         }
         
         //如果不存在该相册创建
         if (group==nil && albumWasFound==NO)
         {
             __weak ALAssetsLibrary* weakSelf = assetsLibrary;
             
             //创建相册
             [assetsLibrary addAssetsGroupAlbumWithName:albumName resultBlock:^(ALAssetsGroup *group)
              {
                  
                  //get the photo's instance
                  [weakSelf assetForURL: assetURL
                            resultBlock:^(ALAsset *asset)
                   {
                       
                       //add photo to the newly created album
                       [group addAsset: asset];
                       
                       //call the completion block
                       completionBlock(nil);
                       
                   } failureBlock: completionBlock];
                  
              } failureBlock: completionBlock];
             return;
         }
         
     }failureBlock:completionBlock];
}

@end
